package com.epro.dx.activity.forgetpassword;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.epro.dx.R;
import com.epro.dx.activity.BaseFragmentActivity;
import com.epro.dx.util.AsyncTaskUtil;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.util.AlertToast;
import com.weipu.common.util.Logger;
import com.weipu.common.util.StringUtil;

public class ForgetPasswordSubmitActivity extends BaseFragmentActivity
		implements OnClickListener {
	private Button forgetPwdSubmitBtn;
	private EditText forgetPwdEmail;
	private String email ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Tracker tracker = DXApplication.getContext().getTracker(TrackerName.APP_TRACKER);
		tracker.setScreenName(ForgetPasswordSubmitActivity.class.getSimpleName());
		tracker.send(new HitBuilders.ScreenViewBuilder().build());
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_forget_password_submit);
		setTitleContent(R.string.forget_password_title);
		setLeftBtnContent(true, -1);
		forgetPwdEmail = (EditText) findViewById(R.id.forget_password_email);
		forgetPwdSubmitBtn = (Button) findViewById(R.id.forget_pwd_btn);
		forgetPwdSubmitBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		email=forgetPwdEmail.getText().toString().trim();
        if(checkEmail(email)){
        	AsyncTaskUtil.executeTask(new LoadingForgot());
        }
	}

	private boolean checkEmail(String email) {
		if (TextUtils.isEmpty(email)) {
			AlertToast.alert(ForgetPasswordSubmitActivity.this,
					R.string.user_register_title_email_null, forgetPwdEmail);
			return false;
		}
		if (!StringUtil.isEmail(email)) {
			AlertToast.alert(ForgetPasswordSubmitActivity.this,
					R.string.user_register_title_email_error, forgetPwdEmail);
			return false;
		}
		if (email.length() < Constant.Rule.EMAIL_MIN_LENGTH
				|| email.length() > Constant.Rule.TEXT_MAX_LENGTH) {
			AlertToast.alert(
					ForgetPasswordSubmitActivity.this,
					getString(R.string.user_register_username_validate_length,
							Constant.Rule.EMAIL_MIN_LENGTH,
							Constant.Rule.TEXT_MAX_LENGTH), forgetPwdEmail);
			return false;
		}
		return true;
	}
	
	protected class LoadingForgot extends AsyncTask<Void, Void, String>
    {
        
        @Override
        protected void onPreExecute()
        {
//            showWaitingDialog();
        	showLoadingLayout(null);
        }
        
        @Override
        protected String doInBackground(Void... params)
        {
            try
            {
            	ProviderFactory.createUserServiceProvider().forgetPassword(email);
            }
            catch (DxException e)
            {
                return e.getErrCode();
            }
            catch (Exception e)
            {
                Logger.e("ForgotActivity", "LoadingForgot", e);
            }
            return "0";
        }
        
        @Override
        protected void onPostExecute(String result)
        {
        	hideAllLayout();
            if (!"0".equals(result))
            {
            	if("800A0207".equals(result)){
            		AlertToast.alert(ForgetPasswordSubmitActivity.this, R.string.mailbox_not_exist, 1000);
            	}else{
            		AlertToast.alert(ForgetPasswordSubmitActivity.this,
    						R.string.client_net_un_available);
            	}
            }
            else
            {
            	Intent intent=new Intent();
            	intent.setClass(ForgetPasswordSubmitActivity.this,ForgetPasswordSuccessActivity.class);
            	intent.putExtra("emailUrl", "");
            	intent.putExtra("email", email);
            	startActivity(intent);
            	finish();
            }
        }
    }

}
